/*
 * =====================================================================================
 *
 *       Filename:  msg.h
 *
 *    Description:  
 *
 *        Version:  1.0
 *        Created:  2014年08月21日 11时55分55秒
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  jianxi sun (jianxi), ycsunjane@gmail.com
 *   Organization:  
 *
 * =====================================================================================
 */
#ifndef __MSG_H__
#define __MSG_H__
#include <stdint.h>
#include <netinet/in.h>
#include "chap.h"
#include "common.h"

#define MSG_TYPE_START (0x11223344)
/* msg type */
enum {
	MSG_AC_BRD  = MSG_TYPE_START,
	MSG_AP_BRD_RESP,
	MSG_AC_REQ_REG,
	MSG_AP_STATUS,
	MSG_AC_CMD,

	MAS_AP_ERR,
	MSG_AP_RESP,
};

struct msg_head_t {
	uint32_t random;
	uint8_t chap[CHAP_LEN];
	char 	acuuid[UUID_LEN];
	unsigned char 	smac[ETH_ALEN];
	uint32_t msg_type;
}__attribute__((packed));

/* ac broadcast; ap response */
struct msg_ac_brd_t {
	struct msg_head_t header;
}__attribute__((packed));

struct msg_ap_brd_resp_t {
	struct msg_head_t header;
	int isreg;
	struct sockaddr_in acaddr;
	char apstatus[0];
}__attribute__((packed));

/* ac request register */
struct msg_ac_req_reg_t {
	struct msg_head_t header;
	struct sockaddr_in acaddr;
	int 	needrestart;
	char regcmd[0];
}__attribute__((packed));

struct msg_ap_status_t {
	struct msg_head_t header;
	char 	status[0];
}__attribute__((packed));

struct msg_ac_cmd_t {
	struct msg_head_t header;
	int 	needrestart;
	char 	cmd[0];
}__attribute__((packed));

void fill_msg_header(struct msg_head_t *, int, 
	char *, uint32_t);

#define CHECK_MSGLEN(msg, len) 				\
do { 							\
	if(len < sizeof(*msg)) { 			\
		sys_err("Receive msg len is error\n"); 	\
		return; 				\
	} 						\
}while(0)

#define CHECK_CHAP(msg, len, random) 			\
do { 							\
	if(chap_msg_cmp_md5((void *)msg, len, random)) { \
		sys_err("Receive packet chap error\n");\
		return; 				\
	} 						\
}while(0)

#define CHECK_UUID(src, dst) 				\
do { 							\
	if(strncmp(src, dst, UUID_LEN - 1)) { 		\
		sys_err("Receive packet uuid error\n"); \
		return; 				\
	} 						\
}while(0)

#endif /* __MSG_H__ */
